package com.service.impl;

import com.dao.AdminBookDao;
import com.entity.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.AdminBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
//1
@Service
public class AdminBookServiceImpl implements AdminBookService {

    @Autowired
    AdminBookDao adminBookDao;

    //得到所有图书类型
    @Override
    public List<BookType> getBookType() {
        List<BookType> bookTypeList= adminBookDao.queryBookType();
//        System.out.println(bookTypeList);
        return bookTypeList;
    }

    //图书信息查询
    @Override
    public PageResult getBookDetail(QueryPageBean queryPageBean, HttpServletRequest request) {
        //得到查询条件
        Integer currentPage=queryPageBean.getCurrentPage();
        Integer pageSize=queryPageBean.getPageSize();
        QueryPageBean.QueryString queryString= queryPageBean.getQueryString();
        String bNo=queryString.getbNo();
        String bName=queryString.getbName();
        String author=queryString.getAuthor();
        String bTypeName=queryString.getbTypeName();

        Map<String,Object> map=new HashMap<>();
        map.put("bNo",bNo);
        map.put("bName",bName);
        map.put("author",author);
        map.put("bTypeName",bTypeName);

        PageHelper.startPage(currentPage,pageSize);
        Page page= adminBookDao.queryAllBooks(map);

        List<Books> rows=page.getResult();
        long total=page.getTotal();
//        for(Books book:rows){
//            System.out.println(book);
//        }

        PageResult pageResult=new PageResult(total,rows);
        return pageResult;
    }

    //根据bNo查询图书信息
    @Override
    public Books getBookById(Integer bNo, HttpServletRequest request) {
        Books book= adminBookDao.queryBookById(bNo);
//        System.out.println(book);
        return book;
    }

    //修改图书信息
    @Override
    public void updateBook(Books book) {
        adminBookDao.updateBook(book);
//        System.out.println(book);
    }

    //查看借者信息
    @Override
    public List<BorrowBooks> getBorrowers(Integer bNo) {
        List<BorrowBooks> borrowers= adminBookDao.queryBorrowers(bNo);
        for(BorrowBooks borrower:borrowers){
            //计算剩余时间
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar=Calendar.getInstance();

            Date nowDate=new Date();
            Date exceptReturn = null;
            try {
                exceptReturn=sdf.parse(borrower.getExpectReturn());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            calendar.setTime(nowDate);
            Integer nowTime=calendar.get(Calendar.DAY_OF_YEAR);

            calendar.setTime(exceptReturn);
            Integer exceptReturnTime=calendar.get(Calendar.DAY_OF_YEAR);

            Integer remindTime=exceptReturnTime-nowTime;
            borrower.setRemindTime(remindTime);
//            System.out.println(remindTime);

        }
        return borrowers;
    }

    //删除图书
    @Override
    public void deleteBook(Integer bNo) {
        //查询是否有读者正在借阅这本书
        int count= adminBookDao.queryBorrowerNumById(bNo);
        if(count>0){
            //有人借阅中，无法删除
            throw  new RuntimeException();
        }else{
            //可以删除
            adminBookDao.deleteBookById(bNo);
        }
//        System.out.println(count);
    }

    //添加图书
    @Override
    public Result addBook(Books book) {
        //查询是否存在
        Books b= adminBookDao.queryBookById(book.getbNo());
        if(b!=null){
            //已经存
            return new Result(false,"已经存在，请重新添加");
        }else{
            //不存在
            book.seteNumber(book.getbTotal());
            adminBookDao.addBook(book);
            return new Result(true,"添加成功");
        }


    }
}
